package com.example.temp;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class PrintTest {


    // wait()、notify/notifyAll() 需要在代码块执行
    static Object o = new Object();
    public static volatile int n=1;

    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    //两个线程交替打印12
    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();
        Thread3 thread3 = new Thread3();
        Thread4 thread4 = new Thread4();
//        Thread t1 = new Thread(run);
//        Thread t2 = new Thread(run);
//        Thread t3 = new Thread(run);
//        t1.start();
//        t2.start();
//        t3.start();

        thread1.start();
        thread2.start();
        thread3.start();

    }

    static class Thread1 extends Thread {

        @Override
        public void run() {

            while (true) {
                synchronized (o) {

                    if (n % 3 == 1) {
                        System.out.println("线程1"+"------>"+n);
                        n++;
                    }
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    o.notifyAll();
                }
            }
        }
    }

    static class Thread2 extends Thread {

        @Override
        public void run() {

            while (true) {
                synchronized (o) {
                    if (n > 100) break;
                    o.notifyAll();
                    if (n % 3 == 2) {
                        System.out.println("线程2"+"------>"+n);
                        n++;
                    }
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    o.notifyAll();
                }
            }
        }
    }

    static class Thread3 extends Thread {

        @Override
        public void run() {

            while (true) {
                synchronized (o) {


                    if (n % 3 == 0) {
                        System.out.println("线程3"+"------>"+n);
                        n++;
                    }

                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    o.notifyAll();
                }
            }
        }
    }

    static class Thread4 extends Thread {

        @Override
        public void run() {
            while (true){
                synchronized (o){
                    lock.lock();
                    if (n==101){
                        System.out.println("线程4----");
                    }

                    lock.unlock();
                }
            }
        }
    }
}



